require("../util.js");
require("../component/components.js");
require("../component/purchaseHeader.js");
require("../component/basicSlide.js");
require("../component/footer.js");
require("../../jsMt/util.js");
var vm = new Vue({
    el:"#newProduct",
    data:{
        newMainList:{},
        newProductList:{},
        newProductList1:[],
        newProductList2:[],
        data:{
            pageNumber:1,
            sortId:"create_date",  //排序字段 综合create_date 价格 integral
            sortValue:""   //排序值 asc 升 desc 降
        },
        isNav:1,
        filter:1
    },
    mounted:function(){
        this.getNewMainList();
        this.getNewProductList();
    },
    methods:{
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        //乐购新品主页接口
        getNewMainList:function(){
            var that=this;
            this.ajax({url:"/purchase/getNewMainList",dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.newMainList=b.json.data;
                }else{
                    vm.$refs.prompt.toast("乐购新品主页失败！")
                }
            });
        },
        //乐购新品商品列表
        getNewProductList:function(){
            var that=this;
            console.log(that.data)
            this.ajax({url:"/purchase/getNewProductList",data:that.data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.newProductList=b.json.data;
                    that.newProductList1=that.newProductList.list;
                    if(that.newProductList.pageNumber>1){
                        if(that.newProductList.list.length>0) {
                            that.newProductList2 = that.newProductList2.concat(b.json.data.list);
                        }
                    }else{
                        that.newProductList2= that.newProductList1;
                    }
                    vm.loadFlag=false;
                    lazyload();
                }else{
                    that.newProductList={};
                    that.newProductList1=[];
                    that.newProductLis2=[];
                    vm.$refs.prompt.toast("获取商品列表失败！")
                }
            });
        },
        //列表筛选
        getFilter:function(num){
            this.filter=num;
            if(this.filter==1){
                this.data.sortId="create_date";
                this.data.sortValue="";
            }else if(this.filter==2){
                this.data.sortId="integral";
                if(this.data.sortValue=="asc"){
                    this.data.sortValue="desc";
                }else if(this.data.sortValue=="desc"){
                    this.data.sortValue="asc";
                }else{
                    this.data.sortValue="asc";
                }
            }
            this.getNewProductList();
        }
    }
});
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
    return scrollTop;
}
//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;

}
var loadFlag;

window.onscroll=function(){
    if(getDocumentTop()>739){
        vm.isNav=2;
    }else{
        vm.isNav=1;
    }
    loadFlag = vm.newProductList.lastPage;
    //监听事件内容
    if(getScrollHeight()==getWindowHeight() + getDocumentTop()){
        //当滚动条到底时,这里是触发内容
        if(!loadFlag){
            vm.loadFlag=true;
            vm.data.pageNumber++;
            vm.getNewProductList();
            lazyload();
        }else{
            vm.$refs.prompt.toast("没有更多了！")
        }
    }
};
function lazyload(){
    $("img.lazy").lazyload();
}